package com.warm.pump.module.work.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.warm.pump.module.work.bean.WorkTree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.module.work.bean.po.gen.Work;
import com.warm.pump.module.work.service.IWorkService;
import com.warm.pump.frame.gen.util.OutJson;
import com.warm.pump.frame.gen.util.MyBeanUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * work
 */
@Controller
@RequestMapping("/work")
public class WorkController {
    @Autowired
    private IWorkService workService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("work:list")
    public OutJson list(Integer page, Integer limit, Work work, String workNameLike, Long jobId) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 999999;
        }
        Map<String, Object> map = new HashMap<>();
        MyBeanUtils.copyBean2Map(map, work);
        if (null != work && work.getSubjectId() != null && 9999999999l == work.getSubjectId()) {
            map.remove("subjectId");
            map.put("subjectId_null", 1);
        }
        if (StringUtils.isNotEmpty(work.getSidx()) && StringUtils.isNotEmpty(work.getOrder())) {
            map.put("orderBy", work.getSidx() + " " + work.getOrder());
        }

        if (StringUtils.isNotEmpty(workNameLike)) {
            map.put("workName_like", workNameLike);
        }

        //查询列表数据
        PageBean pageBean = workService.getPageBeanByMap(map, page, limit, jobId);

        return OutJson.ok().put("page", pageBean);
    }


    /**
     * 获取作业树
     */
    @ResponseBody
    @RequestMapping("/workTreelist")
    public OutJson itemTreelist(Long jobId) {
        List<WorkTree> treeList = workService.getWorkTree();
        return OutJson.ok().setResData(treeList);
    }

    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info/{id}")
    @RequiresPermissions("work:info")
    public OutJson info(@PathVariable("id") Long id) {
        Work work = workService.queryObject(id);

        return OutJson.ok().put("work", work);
    }


    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    @RequiresPermissions("work:save")
    public OutJson save(@RequestBody Work work) {
        work.setCreateTime(new Date());
        work.setUpdateTime(new Date());
        workService.save(work);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("work:update")
    public OutJson update(@RequestBody Work work) {
        work.setUpdateTime(new Date());
        workService.update(work);

        return OutJson.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/updateKeySelective")
    @RequiresPermissions("work:update")
    public OutJson updateKeySelective(@RequestBody Work work) {
        work.setUpdateTime(new Date());
        workService.updateKeySelective(work);

        return OutJson.ok();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("work:delete")
    public OutJson delete(@RequestBody Long[] ids) {
        workService.deleteBatch(ids);

        return OutJson.ok();
    }

    /**
     * 立即执行任务
     */
    @ResponseBody
    @RequestMapping("/run")
    public OutJson run(@RequestBody Long[] ids) {
        workService.run(ids);
        return OutJson.ok();
    }

}
